﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using SEO.Entity;
using System.Data;

namespace SEO.Business
{
    /// <summary>
    /// 公告操作
    /// </summary>
    public class BisNotice
    {
        /// <summary>
        /// 获取公告信息
        /// </summary>
        /// <param name="nID">ID</param>
        /// <returns></returns>
        public Notice Get(int nID)
        {
            SqlDataReader dr = SqlHelper.ExecuteReader(
                                                         SqlHelper.database,
                                                         CommandType.Text,
                                                         "select top 1 * from [Notice] where [ID]=@ID",
                                                         new SqlParameter("@ID", nID)
                                                         );
            Notice Info = null;
            while (dr.Read())
            {
                Info = new Notice();
                Info.ID = Convert.ToInt32(dr["ID"]);
                Info.Title = dr["Title"].ToString();
                Info.Info = dr["Info"].ToString();
                Info.CreateTime = Convert.ToDateTime(dr["CreateTime"]);
            }
            dr.Close();
            return Info;
        }

        /// <summary>
        /// 新增公告信息
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public bool Insert(Notice Info)
        {
            string strSql = "insert into [Notice]([Title],[Info]) values(@Title,@Info)";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@Title", Info.Title));
            parms.Add(new SqlParameter("@Info", Info.Info));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 更新公告信息
        /// </summary>
        /// <param name="Info"></param>
        /// <returns></returns>
        public bool Update(Notice Info)
        {
            string strSql = "update [Notice] set [Title]=@Title,[Info]=@Info where [ID]=@ID";
            List<SqlParameter> parms = new List<SqlParameter>();
            parms.Add(new SqlParameter("@Title", Info.Title));
            parms.Add(new SqlParameter("@Info", Info.Info));
            parms.Add(new SqlParameter("@ID", Info.ID));
            return SqlHelper.ExecuteNonQuery(SqlHelper.database, CommandType.Text, strSql, parms.ToArray()) > 0;
        }

        /// <summary>
        /// 删除公告信息
        /// </summary>
        /// <param name="nID">ID</param>
        /// <returns></returns>
        public bool Delete(int nID)
        {
            return SqlHelper.ExecuteNonQuery(
                   SqlHelper.database,
                   CommandType.Text,
                   "delete from [Notice] where [ID]=@ID;",
                   new SqlParameter("@ID", nID)) > 0;
        }

        /// <summary>
        /// 获取公告列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetList(string strTitle, int nPageIndex, int nPageSize, ref int nPageCount, ref int nRecordCount)
        {
            SqlParameter[] parms = new SqlParameter[6];
            parms[0] = new SqlParameter("@Title", strTitle);
            parms[1] = new SqlParameter("@PageIndex", nPageIndex);
            parms[2] = new SqlParameter("@PageSize", nPageSize);

            //返回值
            parms[3] = new SqlParameter("@PageCount", nPageCount);
            parms[3].Direction = ParameterDirection.Output;
            parms[4] = new SqlParameter("@RecordCount", nRecordCount);
            parms[4].Direction = ParameterDirection.Output;

            DataTable dt = SqlHelper.ExecuteDataset(
                SqlHelper.database,
                CommandType.StoredProcedure,
                "GetNoticeList",
                parms).Tables[0];
            nPageCount = Convert.ToInt32(parms[3].Value);
            nRecordCount = Convert.ToInt32(parms[4].Value);
            return dt;
        }

        /// <summary>
        /// 获取头几个信息
        /// </summary>
        /// <param name="nTop"></param>
        /// <returns></returns>
        public DataTable GetTopList(int nTop)
        {
            string strSql = "select top " + nTop.ToString() + " * from [Notice] order by [ID] desc";
            return SqlHelper.ExecuteDataset(SqlHelper.database,CommandType.Text,strSql).Tables[0];
        }
    }
}
